package org.sxm.cloud.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.sxm.cloud.dto.auth.TokenDTO;
import org.sxm.cloud.entity.order.UserEntity;
import org.sxm.cloud.exception.BizException;
import org.sxm.cloud.service.IAuthService;

@RefreshScope //Nacos动态刷新配置
@RestController
public class AuthController {

    @Autowired
    private IAuthService authService;


    /**
     * 用户登录
     *
     * @param account
     * @param password
     * @return
     */
    @PostMapping("/auth/login")
    public TokenDTO updateOrder(@RequestParam("account") String account, @RequestParam("password") String password) {
        if(StrUtil.isBlank(account)){
            throw new BizException("登录账户不能为空");
        }
        if(StrUtil.isBlank(password)){
            throw new BizException("登录密码不能为空");
        }
        TokenDTO tokenDTO = authService.login(account, password);
        return tokenDTO;
    }

    /**
     *
     * @param token
     * @return
     */
    @PostMapping("/auth/verify")
    public UserEntity verify(@RequestParam("token")String token){
        if(StrUtil.isBlank(token)){
            throw new BizException("用户登录令牌不能为空");
        }
        UserEntity user = authService.verifyToken(token);
        return user;
    }

}
